namespace PHMEE.VibrationDataGenerator.Utils;

public static class CoefUtils
{
    /// <summary>
    /// 计算最小 coef
    /// </summary>
    /// <param name="floatArray"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentException"></exception>
    public static float CalculateMinCoef(float[] floatArray)
    {
        if (floatArray == null || floatArray.Length == 0)
        {
            throw new ArgumentException("Array cannot be null or empty.");
        }

        float maxVal = floatArray[0];
        float minVal = floatArray[0];

        // 找到最大值和最小值
        foreach (var value in floatArray)
        {
            if (value > maxVal)
            {
                maxVal = value;
            }

            if (value < minVal)
            {
                minVal = value;
            }
        }

        // 计算最小 coef
        const short int16Max = short.MaxValue;
        double minCoef = Math.Max(Math.Abs(maxVal), Math.Abs(minVal)) / int16Max;

        return (float)minCoef;
    }
}